package com.example.whereismystuff.classes;


import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LoginDataBaseAdapter 
{
	static final String DATABASE_NAME = "login.db";
	public static final String KEY_ID = "_id";
	static final int DATABASE_VERSION = 1;
	public static final int NAME_COLUMN = 1;
	// TODO: Create public field for each column in your table.
	// SQL Statement to create a new database.
	/*static final String DATABASE_CREATE = "create table "+"LOGIN"+
			"( " +"_id"+" integer primary key autoincrement,"+ "USERNAME  text,PASSWORD text, LOCKED text, ADMIN text); ";*/
	static final String DATABASE_CREATE = "CREATE TABLE LOGIN ( _id integer primary key autoincrement, USERNAME TEXT," +
			"PASSWORD TEXT, LOCKED TEXT, ADMIN TEXT, EMAIL TEXT, ITEMS TEXT)";
	
	
	
	
	
	// Variable to hold the database instance
	public  SQLiteDatabase db;
	// Context of the application using the database.
	private final Context context;
	// Database open/upgrade helper
	private DataBaseHelper dbHelper;
	/**
	 * Constructor
	 * @param _context
	 */
	public  LoginDataBaseAdapter(Context _context) 
	{
		context = _context;
		dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	/**
	 * Opens the database
	 * @return LoginDataBaseAdapter
	 * @throws SQLException
	 */
	public  LoginDataBaseAdapter open() throws SQLException 
	{
		db = dbHelper.getWritableDatabase();
		return this;
	}
	/**
	 * Closes the database
	 */
	public void close() 
	{
		db.close();
	}
	/**
	 * Get the Database
	 * @return SQLiteDatabase
	 */
	public  SQLiteDatabase getDatabaseInstance()
	{
		return db;
	}
	
	/**
	 * Insert a User
	 * @param userName
	 * @param password
	 * @param locked
	 * @param admin
	 * @return
	 */
	public long insertEntry(String userName,String password, String locked, String admin)
	{
		ContentValues newValues = new ContentValues();
		// Assign values for each row.
		newValues.put("USERNAME", userName);
		newValues.put("PASSWORD",password);
		newValues.put("LOCKED",locked);
		newValues.put("ADMIN",admin);

		// Insert the row into your table
		long uniqueID = db.insert("LOGIN", null, newValues);
		///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
		return uniqueID;
	}
	/**
	 * Insert a User into the Database
	 * @param user
	 */
	public void insertEntry(User user)
	{
		ContentValues values=new ContentValues(6);
		int tempBool;
		//user.setUniqueID(ID);
		//values.put("id", ID++);//Unique ID
		
		values.put("USERNAME",user.getUserName());
		values.put("PASSWORD",user.getPassword());
		values.put("LOCKED", tempBool = user.isBlocked()? 1 : 0);
		values.put("ADMIN", tempBool = user.isAdmin()? 1 : 0);
		values.put("EMAIL",user.getEmail());
		
		String tempItems = "";
		
		/*ArrayList<Integer> itemList = user.IDList;
		if(user.IDList == null)
		{
			for(int i = 0; i < itemList.size();i++)
				tempItems += itemList.get(i) + " ";
		}*/
		values.put("ITEMS", tempItems);
		
		long uniqueID = db.insert("LOGIN", null, values);
		user.setUniqueID((int)uniqueID);
	}
	/**
	 * Deletes an entry from the Database
	 * @param UserName
	 * @return
	 */
	public int deleteEntry(String UserName)
	{
		//String id=String.valueOf(ID);
		String where="USERNAME=?";
		int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
		// Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
		return numberOFEntriesDeleted;
	}

	/**
	 * Search to see if the database has a User
	 * @param userName
	 * @return true if the user is found, false otherwise.
	 */
	public boolean containsUser(String userName) 
	{
		Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
		return !(cursor.getCount() < 1);
	}
	/**
	 * Gets the UserName
	 * @param userName
	 * @return
	 */
	public String getSingleEntry(String userName)
	{
		Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{userName}, null, null, null);
		if(cursor.getCount()<1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
		cursor.close();
		return password;				
	}
	/**
	 * Gets if the user being searched is Locked
	 * @param userName
	 * @return true or false
	 */
	public String getSingleEntryLock(String userName)
	{
		Cursor cursor=db.query("LOGIN", null, "USERNAME=?", new String[]{userName}, null, null, null);
		if(cursor.getCount()<1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String locked= cursor.getString(cursor.getColumnIndex("LOCKED"));
		cursor.close();
		return locked;				
	}
	/**
	 * Checks if the Username is an Admin
	 * @param userName
	 * @return true or false
	 */
	public String getSingleEntryAdmin(String userName)
	{
		Cursor cursor=db.query("LOGIN", null, "USERNAME=?", new String[]{userName}, null, null, null);
		if(cursor.getCount()<1) // UserName Not Exist
		{
			cursor.close();
			return "NOT EXIST";
		}
		cursor.moveToFirst();
		String locked= cursor.getString(cursor.getColumnIndex("ADMIN"));
		cursor.close();
		return locked;				
	}
	
	/**
	 * Updates the User in the Database
	 * @param userName
	 * @param password
	 * @param locked
	 * @param admin
	 */

	public void  updateEntry(String userName,String password, String locked, String admin)
	{
		// Define the updated row content.
		ContentValues updatedValues = new ContentValues();
		// Assign values for each row.
		updatedValues.put("USERNAME", userName);
		updatedValues.put("PASSWORD",password);
		updatedValues.put("LOCKED", locked);
		updatedValues.put("ADMIN", admin);

		String where="USERNAME = ?";
		db.update("LOGIN",updatedValues, where, new String[]{userName});			   
	}
	public void  updateEntry(User user)
	{
		// Define the updated row content.
		ContentValues updatedValues = new ContentValues();
		// Assign values for each row.
		updatedValues.put("USERNAME", user.getUserName());
		updatedValues.put("PASSWORD",user.getPassword());
		updatedValues.put("LOCKED", user.isBlocked());
		updatedValues.put("ADMIN", user.isAdmin());
		updatedValues.put("EMAIL",user.getEmail());
		
		String tempItems = "";
		/*if(user.IDList == null)
		{
			ArrayList<Integer> itemList = user.IDList;
			for(int i = 0; i < itemList.size();i++)
				tempItems += itemList.get(i) + " ";
		}*/
		updatedValues.put("ITEMS", tempItems);

		String where="USERNAME = ?";
		db.update("LOGIN",updatedValues, where, new String[]{user.getUserName()});			   
	}
	/**
	 * Gets all the users
	 * @return
	 */
	public Cursor queueAll(){
		String[] columns = new String[]{"_id", "USERNAME", "PASSWORD", "LOCKED", "ADMIN"};
		Cursor cursor = db.query("LOGIN", columns,null, null, null, null, null);
		return cursor;
	}
	/**
	 * Returns all the Usernames inside the Database
	 * @return
	 */
	public ArrayList<String> queueAllUsers() {
		String[] columns = new String[]{"USERNAME"};
		Cursor cursor = db.query("LOGIN", columns, 
				null, null, null, null, null);
		ArrayList<String> result = new ArrayList<String>();

		int index_CONTENT = cursor.getColumnIndex("USERNAME");
		for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
			result.add(cursor.getString(index_CONTENT));
		}
		return result;
	}
}

